package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.mapper.MenuMapper;
import com.qianfeng.openapi.web.master.pojo.Menu;
import com.qianfeng.openapi.web.master.service.MenuService;
import com.qianfeng.openapi.web.master.utils.Constacts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LJF
 * @Date 2020/7/1 16:55
 * @Version 1.0
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /** 左侧菜单栏 */
    @Override
    public List<Menu> findMenuByUid(Integer uid) {
        List<Menu> menuList = menuMapper.findMenuByUid(uid);

        return changeMenuList(menuList);
    }

    private List<Menu> changeMenuList(List<Menu> menuList) {
        List<Menu> list = new ArrayList<>();    //存储一级菜单
        Map<Integer, Menu> menuMap = new HashMap<>();    //存储所有的菜单

        for (Menu menu : menuList) {
            menuMap.put(menu.getId(), menu);
            if (menu.getParentId() == null) {
                //如果是一级菜单
                list.add(menu);
            }
        }

        for (Menu menu : menuList) {
            if (menu.getType() != Constacts.MENU_NOT_BUTTON) {
                if (menu.getParentId() != null && menuMap.containsKey(menu.getParentId())) {
                    menuMap.get(menu.getParentId()).getChildren().add(menu);
                }
            }
        }

        return list;
    }

    /** 授权管理 */
    @Override
    public List<Menu> getFullMenu() {
        List<Menu> allMenu = menuMapper.getAllMenu();   //查询所有的菜单
        List<Menu> menuList = new ArrayList<>();    //存储一级菜单
        Map<Integer, Menu> menuMap = new HashMap<>();    //存储所有的菜单

        for (Menu menu : allMenu) {
            menuMap.put(menu.getId(), menu);
            if (menu.getParentId() == null) {
                //如果是一级菜单
                menuList.add(menu);
            }
        }

        for (Menu menu : allMenu) {
            if (menu.getParentId() != null && menuMap.containsKey(menu.getParentId())) {
                menuMap.get(menu.getParentId()).getChildren().add(menu);
            }
        }
        return menuList;
    }

    @Override
    public List<Menu> getAllMenu() {
        return menuMapper.getAllMenu();
    }

    @Override
    public List<Menu> getMenuTree() {
        List<Menu> menus = menuMapper.getAllMenu();
        return changeMenuList(menus);
    }

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.getMenuById(id);
    }

    @Override
    public int insertMenu(Menu menu) {
        return menuMapper.insertMenu(menu);
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public int deleteMenus(Integer[] ids) {
        return menuMapper.deleteMenuByIds(ids);
    }
}
